package leetcode;

import java.util.List;

/**
 * @author niann
 * @description 反转链表
 * //TODO  https://leetcode.cn/problems/reverse-linked-list/description/
 * @since 2024/8/28 下午7:16
 **/
public class LeetCode206 {


     public class ListNode {
         int val;
         ListNode next;
     }

    /**
     * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
     *
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head==null||head.next==null){
            return head;
        }
        var cur = head;
        var next = head.next;
        ListNode prev = null;
        while (next != null) {
            cur.next= prev;
            prev=cur;
            cur= next;
            next=cur.next;
        }
        cur.next = prev;
        return cur;
    }
}
